/* 
 * Copyright (c) 2009, Giora Kosoi giorak@gmail.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Giora Kosoi ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Giora Kosoi BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sparrow/variant_ipv4.h>
#include <sparrow/errors.h>

VariantIpV4::VariantIpV4(const in_addr &value)
:   Variant(VT_IPV4),
    value_(value)
{
}

VariantIpV4::VariantIpV4(const std::string &value)
:   Variant(VT_IPV4)
{
    str(value);
}

VariantIpV4::VariantIpV4(const Octets &value)
:   Variant(VT_IPV4)
{
    octets(value);
}

VariantIpV4::VariantIpV4(const uint8_t *value, size_t value_len)
:   Variant(VT_IPV4)
{
    octets(value, value_len);
}

VariantIpV4::VariantIpV4(const Octets::const_iterator &begin, const Octets::const_iterator &end)
    :   Variant(VT_IPV4)
{
    octets(begin, end);
}

VariantIpV4::~VariantIpV4()
{
}

const in_addr &VariantIpV4::ipv4() const
{
    return value_;
}

void VariantIpV4::ipv4(const in_addr &value)
{
    value_ = value;
    o_.clear();
    s_.clear();
}

const Octets &VariantIpV4::octets() const
{
    if(o_.empty())
    {
        o_.insert(o_.begin(), (uint8_t *)&value_, (uint8_t *)&value_ + sizeof(value_));
    }
    return o_;
}

void VariantIpV4::octets(const uint8_t *value, size_t value_len)
{
    if(value == 0 || value_len != sizeof(value_))
    {
        throw_error<std::runtime_error>(__FILE__, __LINE__, "value not convertible to ipv4");
    }
    memcpy(&value_, value, sizeof(value_)); 
    s_.clear();
    o_.clear();

}

void VariantIpV4::octets(const Octets::const_iterator &begin, const Octets::const_iterator &end)
{
    octets(&(*begin), end - begin);
}

void VariantIpV4::octets(const Octets &value)
{
    octets(&value.at(0), value.size());
}

const std::string &VariantIpV4::str() const
{
    if(s_.empty())
    {
        char buff[INET_ADDRSTRLEN] = { 0 };
        const char *dots = inet_ntop(AF_INET, (const void *)&value_, buff, INET_ADDRSTRLEN);
        if(dots == 0)
        {
            throw_error<std::runtime_error>(__FILE__, __LINE__, "address not convertible to string");
        }
        s_.assign(dots);
    }
    return s_;
}

const std::string &VariantIpV4::pstr() const
{
    return str();
}

void VariantIpV4::str(const std::string &value)
{
    in_addr tmp_ip; 
    int result = inet_pton(AF_INET, value.c_str(), (void *)&tmp_ip);
    if(result <= 0)
    {
        throw_error<std::runtime_error>(__FILE__, __LINE__, "value [%s] not convertible to ip", value.c_str());
    }
    s_.clear();
    o_.clear();
    value_ = tmp_ip;
}

Variant *VariantIpV4::clone() const
{
    return new VariantIpV4(value_);
}

